package cn.edu.hqu.cst.android.hxy.e6;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.URLUtil;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class CustomBrowser extends Activity {

    WebView wv;
    SharedPreferences share;
    SharedPreferences.Editor shareEditor;
    ArrayList<String> history = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 显示进度条
        getWindow().requestFeature(Window.FEATURE_PROGRESS);

        setContentView(R.layout.activity_main);

        final ListView lv = findViewById(R.id.lv);
        share = getSharedPreferences("com.example.custombrowser.history", MODE_PRIVATE);
        int count = share.getInt("count", 0);
        String[] urls = new String[count];
        history.clear();
        for (int i = 0; i < count; i++) {
            urls[i] = share.getString("url_" + i, "");
            history.add(urls[i]);
        }

        ArrayAdapter<String> ad = new ArrayAdapter<>(this,
                android.R.layout.simple_list_item_1,
                urls);
        lv.setAdapter(ad);

        shareEditor = share.edit();

        wv = findViewById(R.id.wv);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int newProgress) {
                setProgress(newProgress * 100);
            }
        });

        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (!history.contains(url)) {
                    history.add(url);
                }
            }
        });

        EditText et = findViewById(R.id.et);

        Button btn = findViewById(R.id.btn);
        btn.setOnClickListener(v -> {
            lv.setVisibility(View.GONE);
            wv.setVisibility(View.VISIBLE);
            String url = et.getText().toString().trim();
            if (URLUtil.isNetworkUrl(url)) {
                wv.loadUrl(url);
            } else {
                Toast.makeText(CustomBrowser.this, "sorry,您输入的网址有错",
                        Toast.LENGTH_SHORT).show();
                et.requestFocus();
            }
        });

        Button btnForward = findViewById(R.id.btnForward);
        btnForward.setOnClickListener(v -> {
            if (wv.canGoForward()) {
                wv.goForward();
            } else {
                Toast.makeText(CustomBrowser.this, "已经到最后一页，不能前进了",
                        Toast.LENGTH_SHORT).show();
            }
        });

        Button btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(v -> {
            if (wv.canGoBack()) {
                wv.goBack();
            } else {
                Toast.makeText(CustomBrowser.this, "已经到第一页，不能后退了",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        shareEditor.clear();
        for (int i = 0; i < history.size(); i++) {
            shareEditor.putString("url_" + i, history.get(i));
        }
        shareEditor.putInt("count", history.size());
        shareEditor.apply();
        super.onDestroy();
    }
}
